home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 07 - Mechanics - Environment / TextGrid / TextGrid.c < prev    next >
C/C++ Source or Header  |  1995-03-02  |  2KB  |  108 lines

  1.  
  2.  
  3. /*Size of the array*/
  4. #define    kArraySizeH 15
  5. #define    kArraySizeV 12
  6.  
  7. /*Size of the tiles*/
  8. #define    kTileSizeH 32
  9. #define    kTileSizeV 32
  10.  
  11. /*The world is accessed through a Handle to a 'TEXT' resource:*/
  12. Handle    gWorldHandle;
  13.  
  14. /*…and this is the resource number:*/
  15. #define    kWorldResource    128
  16.  
  17. /* Pictures*/
  18. PicHandle floorTile;
  19. PicHandle playerTile;
  20. PicHandle enemyTile;
  21. PicHandle goldTile;
  22. PicHandle wallTile;
  23. PicHandle exitTile;
  24.  
  25. /* Draw a tile */
  26.  
  27. static void DrawTile(short h, short v)
  28. {
  29.     Rect    tileRectangle;
  30.     char    theTile;
  31.     
  32.     SetRect(&tileRectangle, h * kTileSizeH, v * kTileSizeV, (h + 1) * kTileSizeH, (v + 1) * kTileSizeV);
  33.     
  34.     theTile = *(*gWorldHandle + h + v * (kArraySizeH+1));
  35.     
  36.     switch (theTile)
  37.     {
  38.         case ' ':
  39.             DrawPicture(floorTile, &tileRectangle); break;
  40.         case '#':
  41.             DrawPicture(wallTile, &tileRectangle); break;
  42.         case '@': 
  43.             DrawPicture(playerTile, &tileRectangle); break;
  44.         case '*':
  45.             DrawPicture(enemyTile, &tileRectangle); break;
  46.         case '$': 
  47.             DrawPicture(goldTile, &tileRectangle); break;
  48.         case 'X':
  49.             DrawPicture(exitTile, &tileRectangle); break;
  50.         default:
  51.             PaintRect(&tileRectangle);
  52.     }
  53. } /*DrawTile*/
  54.  
  55.  
  56. /* Standard inits */
  57.  
  58. static void InitToolbox(void) {
  59.     InitGraf (&qd.thePort);
  60.     InitFonts ();
  61.     FlushEvents (everyEvent,0);
  62.     InitWindows ();
  63.     InitMenus ();
  64.     TEInit ();
  65.     InitDialogs (nil);
  66.     InitCursor ();
  67. }
  68.  
  69.  
  70. /****************** Main program ******************/
  71.  
  72. void main(void) {
  73.     WindowPtr    myWindow;
  74.     Rect    windowRectangle;
  75.     short    h,v;
  76.  
  77.     InitToolbox();
  78.  
  79. /*Set up the window*/
  80.     SetRect(&windowRectangle, 50, 50, 50 + kArraySizeH * kTileSizeH, 50 + kArraySizeV * kTileSizeV);
  81.     myWindow = NewCWindow(nil, &windowRectangle, "\pText grid demo", true, 0, (WindowPtr)-1L, false, 0);
  82.     SetPort(myWindow);
  83.  
  84. /*Load all pictures*/
  85.     floorTile = GetPicture(128);            /*PICT resource #128.*/
  86.     playerTile = GetPicture(129);            /*PICT resource #129.*/
  87.     enemyTile = GetPicture(130);            /*PICT resource #130.*/
  88.     goldTile = GetPicture(131);                /*PICT resource #131.*/
  89.     wallTile = GetPicture(132);                /*PICT resource #132.*/
  90.     exitTile = GetPicture(133);                /*PICT resource #133.*/
  91.  
  92. /*Get the resource*/
  93.     gWorldHandle = GetResource('TEXT', kWorldResource);
  94.     if (gWorldHandle == nil)    /*Error! In a real program, you should check the picts above as well.*/
  95.     {
  96.         SysBeep(1);
  97.         ExitToShell();
  98.     }
  99.  
  100. /*Draw all tiles!*/
  101.     for ( h = 0 ; h < kArraySizeH ; h++)
  102.         for ( v = 0 ; v < kArraySizeV ; v++)
  103.             DrawTile(h, v);
  104.  
  105.     while ( ! Button () )
  106.         ;
  107. } /*TextGrid*/
  108.